from django.db import models
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist

# 1-1 relationship with User described here:
#	http://sam.odio.com/blog/2008/05/extending-user-model-profiles-in-django.php
class Account(models.Model):
	user = models.OneToOneField(User, unique=True)

	address1 = models.CharField(default='', max_length=200)
	address2 = models.CharField(default='', max_length=200)
	city = models.CharField(default='', max_length=200)
	state = models.CharField(default='', max_length=200)
	country = models.CharField(default='', max_length=200)
	postalcode = models.IntegerField(default=-1, max_length=20)
	
	birthday = models.DateField(null=True, blank=True)
	phone = models.IntegerField(default=-1, max_length=15)
	website = models.URLField(verify_exists=True)
	resume = models.FileField(default='', upload_to='resumes', null=True, blank=True)

	view_email = models.BooleanField(default=False)
	view_phone = models.BooleanField(default=False)
	view_website = models.BooleanField(default=False)
	view_resume = models.BooleanField(default=False)
	view_address = models.BooleanField(default=False)
	view_city = models.BooleanField(default=False)
	view_state = models.BooleanField(default=False)
	view_country = models.BooleanField(default=False)
	
	last_modified = models.DateTimeField(auto_now_add=True, auto_now=True)
	
	personal_statement = models.TextField()

def get_or_create_account(user):
	try:
		account = user.get_profile()
	except ObjectDoesNotExist:
		account = Account(user=user)
		account.save()
	return account
	

#	1-M relationship with User
class Job(models.Model):
	owner = models.ForeignKey(User)
	title = models.CharField(max_length=200)
	company = models.CharField(max_length=200)
	pay = models.DecimalField(max_digits=10, decimal_places=2)
	description = models.TextField()
	last_modified = models.DateTimeField(auto_now_add=True, auto_now=True)

#	M-M between User and Job
class FavoriteJob(models.Model):
	job = models.ForeignKey(Job)
	account = models.ForeignKey(User)
	unique_together = (("job", "account"),)

#	M-M between User and User
#	is this bad? Maybe we need to create employer and recruit tables
class FavoriteRecruit(models.Model):
	employer = models.ForeignKey(User, related_name='favorite_recruit')
	job_recruit = models.ForeignKey(User)
	
class Filter(models.Model):
	description = models.TextField(max_length=200, unique=True)
	
class JobQualities(models.Model):
	filter = models.ForeignKey(Filter)
	job = models.ForeignKey(Job)
	